package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.DamageListGoodsMapper;
import com.atguigu.jxc.dao.GoodsMapper;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DamageListGoodsServiceimpl implements DamageListGoodsService {

    @Autowired
    DamageListGoodsMapper damageListGoodsMapper;

    @Autowired
   GoodsMapper goodsMapper;


    //保存报损单
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(DamageList damageList, String damageListGoodsStr) {
           //使用谷歌Gson将json字符串数组转换成具体的集合
        Gson gson=new Gson();
        List<DamageListGoods> listGoods = gson.fromJson(damageListGoodsStr, new TypeToken<List<DamageListGoods>>() {
        }.getType());
           //设置当前操作用户
         damageList.setUserId(1);
         //保存商品报损单信息
           damageListGoodsMapper.save(damageList);
           //保存商品报损单商品信息

        for (DamageListGoods listGood : listGoods) {
                 listGood.setDamageListId(damageList.getDamageListId());
                 //进行保存
              damageListGoodsMapper.savegoods(listGood);
            System.out.println(listGood);

            //修改商品状态
               //获取id
            Integer goodsId = listGood.getGoodsId();
             if (goodsId!=null){
                 //进行修改 0表示初始值,1表示已入库，2表示有进货或销售单据
                 Goods selectont = goodsMapper.selectont(goodsId);
                 //修改库存数量
                 selectont.setInventoryQuantity(selectont.getInventoryQuantity()-listGood.getGoodsNum());
                   //状态
                    selectont.setState(2);
                    //进行修改
                   goodsMapper.update(selectont);
             }
             //新增日志略

        }




    }
//报损单查询
    @Override
    public List<DamageListGoods> list(String sTime, String eTime) {
            return damageListGoodsMapper.list(sTime,eTime);
    }

    //查询报损单商品信息
    @Override
    public List<DamageListGoods> goodsList(Integer damageListId) {
                return damageListGoodsMapper.goodsList(damageListId);
    }
}
